﻿$(function () {
    $.kish.CarStoresOfCurrentUser = {};
    $.extend($.kish.CarStoresOfCurrentUser, {
        WithTotal: null,
        initializing: function () {
            $.kish.CarStoresOfCurrentUser.ListCarStore(0, 20, true);
        },

        //*************************//
        ListCarStore: function (start, end, withtotal) {
            $.ajax({
                url: '../../KishCar/Services/CarAnn.svc/GetCarStoresOfCurrentUser',
                data: JSON.stringify({ Start: start, End: end, WithTotal: withtotal }),
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function (data) {

                    if (data['d'] != null) {
                        if (data['d'].length == 0) {
                            $.kish.message.getErrorMessage("15");
                        }
                        else {
                            $('.carStoreTable').html('');
                            var tableHeader = '<tr><th >تصویر</th><th >نام</th><th >تلفن</th><th>آدرس</th><th>پست الکترونیکی</th><th>وضعیت</th><th>ویرایش</th><th>حذف</th></tr>';
                            $('.carStoreTable').append(tableHeader);
                            for (var i = 0; i < data['d'].Data.length; i++) {
                                var path = '';
                                if (data['d'].Data[i].File != null) {
                                    path = (data['d'].Data[i].File.Path);
                                    var relatedPath = '../' + path.replace('~', '..');
                                    var Image = '<a  class="highslide" href="' + relatedPath + '"> <img  class="width80 fn_prodImg " alt="' + data['d'].Data[i].Name + '" title="تصویر بزرگتر" src= "' + relatedPath + '"/></a>';
                                }
                                var Satus = data['d'].Data[i].IsActive ? "تایید شده" : "تایید نشده"
                                var x = '<tr id=' + data['d'].Data[i].ID + '><td >' + (path ? Image : "بدون تصویر") + ' </td><td class=" breakWord">' + data['d'].Data[i].Name + ' </td ><td>' + data['d'].Data[i].Telephone + '</td><td>' + data['d'].Data[i].Address + '</td><td>' + data['d'].Data[i].Email + '</td><td>' + Satus + '</td>' +
                                '<td><img alt="ویرایش" title="ویرایش" class="noBorder  inline-block pointer fn_CarStoreEdit" src="../../Images/ico/edit.gif" /></td>' +
                                '<td><img alt="حذف" title="حذف" class="noBorder  inline-block pointer fn_CarStoreDelete" src="../../Images/ico/delete.gif" /></td></tr>';
                                $('.carStoreTable').append(x);
                                $('.carStoreTable tr:last').data('DTO', data['d'].Data[i])
                            };

                            $('.fn_CarStoreEdit').click(function () {
                                var carStoreID = $(this).closest("tr").attr('id');
                                window.location.replace("./EditCarStore.aspx?ID=" + carStoreID);
                            });

                            $('.fn_CarStoreDelete').click(function () {
                                var carStoreID = $(this).closest("tr").attr('id');
                                $.kish.CarStoresOfCurrentUser.DeleteCarStore(carStoreID, $(this).closest("tr"));
                            });

                            $('a.highslide').click(function () {
                                return hs.expand(this)
                            });

                            if (withtotal && data['d'].TotalCount && data['d'].TotalCount > 0 && data['d'].TotalCount != null) {
                                $.kish.CarStoresOfCurrentUser.WithTotal = data['d'].TotalCount;
                                $.kish.CarStoresOfCurrentUser.SetPaging();
                            }
                        }
                    }
                }
            });
        },

        SetPaging: function () {
            var withTotal = $.kish.CarStoresOfCurrentUser.WithTotal;
            var PageCount = Math.ceil(withTotal / 20);
            var paging = '';
            for (var i = 0; i < PageCount; i++) {
                var PageItem = '<span class="pagingItem">' + (i + 1).toString() + '</span>';
                paging = paging + PageItem;
            }
            $('.PagingDiv').text('');
            $('.PagingDiv').append(paging);
            $('.PagingDiv').append('<span class="totalCount"> کل موارد یافت شده : ' + withTotal + '</span>');
            $('.pagingItem', '.PagingDiv').click(function () {
                var pageIndex = $(this).text();
                $.kish.CarStoresOfCurrentUser.ListCarStore((pageIndex - 1) * 20, pageIndex * 20, false);
            });
        },

        DeleteCarStore: function (carStoreID,row) {
            $.ajax({
                url: '../../KishCar/Services/CarAnn.svc/DeleteCarStore',
                data: JSON.stringify({ CarStoreID: carStoreID }),
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function (result) {
                    if (result['d'].IsSucceed) {
                        $.kish.message.showUserMessage(result['d'].Message.Message);
                        row.remove();
                    }
                    else {
                        $.kish.message.showErrorMessage(result['d'].Message.Message);
                    };
                    $('#message').slideDown();
                    $('img.loader').fadeOut('slow', function () { $(this).remove() });
                    $('#submit').removeAttr('disabled', 'disabled');
                }
            });
        }
    });
    $.kish.CarStoresOfCurrentUser.initializing();
});